﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class ACC_Navigate2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");

        if (Request.QueryString["1"] != null && Page.IsPostBack != true)
        {
            //Set the EntryPoint Selection from the QueryString
            EntryPointSelect.SelectedIndex = -1;
            ListItem li = EntryPointSelect.Items.FindByValue(Request.QueryString["1"]);
            if (li != null)
                li.Selected = true;
            //Fire the EntryPointSelect Event
            EntryPointSelect_SelectedIndexChanged(EntryPointSelect, e);
        }

        Session["RollupFilters1"] = RollupFilters1;
        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }


    protected void btnFilter_Click(object sender, EventArgs e)
    {
        EntryPointSelect.Items.FindByText("Portfolios").Selected = true;
        EntryPointSelect_SelectedIndexChanged(sender, e);
    }

    protected void btnFilterProperties_Click(object sender, EventArgs e)
    {
        FirstLevelList_SelectedIndexChanged(sender, e);
    }

    protected void EntryPointSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");

        FirstLevelList.Items.Clear();
        FirstLevel.Visible = true;

        //imgbtnShow3.Visible = false;
        //imgbtnShow3.Visible = false;
        lblRollupOptions.Visible = imgbtnShow3.Visible;
        imgbtnRefresh1.Visible = false;
        lblRefresh.Visible = imgbtnRefresh1.Visible;
        imgbtnFilterPortfolios.Style["display"] = "none";
        Session["PropertiesForName"] = "";

        FirstListSearching.Enabled = false;
        FirstLevelList.Items.Add("Select " + EntryPointSelect.SelectedValue);

        SecondLevelList.Items.Clear();
        SecondLevel.Visible = false;
        ThirdLevel.Style["display"] = "none";
        try
        {
            switch (EntryPointSelect.SelectedValue)
            {
                //case "Manager":
                //case "Managers":
                //    FirstLevelList.DataSourceID = "";
                //    FirstLevelList.DataSource = (new SQLServer()).TempManagersList();
                //    FirstLevelList.DataBind();
                //   break;

                case "Portfolio":
                case "Portfolios":
                    //imgbtnFilterPortfolios.Style["display"] = "";
                    FirstListSearching.Enabled = true;
                    FirstLevelList.DataSourceID = "";

                    FirstLevelList.DataSource = (new Menus()).dvACC_DATA("Portfolio");
                    FirstLevelList.DataBind();

                    lblFirstLevel.CssClass += " right";
                    break;



                case "Report":
                    imgbtnShow3.Visible = false;       
                    lblRollupOptions.Visible = imgbtnShow3.Visible;  
                  
                    break;

                default:
                    //    FirstLevel.Visible = false;
                    //    SecondLevel.Visible = false;
                    //    lblFirstLevel.Text = "";
                    //    lblSecondLevel.Text = "";
                    break;
            }
        }
        catch (Exception ex)
        {
            ReportFilters1.ErrorMessage = ex.Message;
        }

        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    protected void FirstLevelList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");
        String[] AMInfo;


        lblSecondLevel.Text = "Properties";
        SecondLevelList.Items.Clear();
        SecondLevelList.Items.Add("Select " + lblSecondLevel.Text);
        SecondLevel.Visible = true;
        Session["PropertiesForName"] = "";

        imgbtnRefresh1.Visible = FirstLevelList.SelectedIndex > 0;
        lblRefresh.Visible = imgbtnRefresh1.Visible;
        lblFirstLevel.Visible = imgbtnRefresh1.Visible;
        imgbtnShow3.Visible = imgbtnRefresh1.Visible;
        imgbtnShow3.Visible = imgbtnRefresh1.Visible;
        lblRollupOptions.Visible = imgbtnShow3.Visible;
        //ThirdLevel.Visible = true;

        //////TO DO: Convert this to Pull from cache
        //string sPropertySelect = "SELECT PROPERTY_NM||' ('|| TRIM(TRAC_ID) ||')' PROPERTY";
        //if (chkByTracID.Checked) sPropertySelect = "SELECT TRIM(TRAC_ID) ||' - ' || PROPERTY_NM PROPERTY";
        try
        {
            switch (EntryPointSelect.SelectedValue)
            {
                case "Manager":
                    AMInfo = FirstLevelList.SelectedValue.Split(':');

                    if (AMInfo.Length > 1)
                    {
                        switch (AMInfo[1])
                        {
                            case "0":
                                //GenlManagers oGenMgr = new GenlManagers();
                                //SecondLevelList.DataSource = oGenMgr.PropertiesList(AMInfo[0]);
                                //SecondLevelList.DataBind();
                                SecondLevelList.DataSource = (new Regions()).PropertiesList(FirstLevelList.SelectedValue.ToString().Split(':')[0]);
                                SecondLevelList.DataBind();
                                NoPropertyList();
                                break;
                            case "1":
                                //AreaManagers oAreaMgr = new AreaManagers();
                                //SecondLevelList.DataSource = oAreaMgr.PropertiesList(AMInfo[0]);
                                //SecondLevelList.DataBind();
                                SecondLevelList.DataSource = (new Markets()).PropertiesList(FirstLevelList.SelectedValue.ToString().Split(':')[0]);
                                SecondLevelList.DataBind();
                                break;
                            case "2":
                                AcctManagers oAcctMgr = new AcctManagers();
                                SecondLevelList.DataSource = oAcctMgr.PropertiesList(AMInfo[0]);
                                SecondLevelList.DataBind();
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        SecondLevel.Visible = false;
                    }

                    break;

                case "State":
                    if (FirstLevelList.SelectedValue.Trim() == "ZZ")
                        NoPropertyList();
                    else
                    {
                        States oState = new States();
                        SecondLevelList.DataSource = oState.PropertiesList(FirstLevelList.SelectedValue);
                        SecondLevelList.DataBind();
                    }
                    break;

                case "Market":
                    Markets oMkt = new Markets();
                    SecondLevelList.DataSource = oMkt.PropertiesList(FirstLevelList.SelectedValue);
                    SecondLevelList.DataBind();
                    break;

                case "Portfolio":
                case "Portfolios":
                    Portfolios oPortfolio = new Portfolios();
                    SecondLevelList.DataSource = oPortfolio.PropertiesList(FirstLevelList.SelectedValue);
                    SecondLevelList.DataBind();
                    //lblSecondLevel.Text = "Properties ";
                    break;

                case "Region":
                    Regions oRegion = new Regions();
                    //if (FirstLevelList.SelectedValue.Trim() == "ZZ")
                    //    NoPropertyList();
                    //else
                    //{ }
                    SecondLevelList.DataSource = oRegion.PropertiesList(FirstLevelList.SelectedValue);
                    SecondLevelList.DataBind();
                    NoPropertyList();
                    break;

                default:
                    NoPropertyList();
                    imgbtnRefresh1.Visible = false;
                    lblRefresh.Visible = false;

                    imgbtnShow3.Visible = false;
                    imgbtnShow3.Visible = false;
                    lblRollupOptions.Visible = false;
                    ThirdLevel.Style["display"] = "none";
                    break;
            }
       
            if ((EntryPointSelect.SelectedValue == "Report") && FirstLevelList.SelectedIndex > 0)
            {
            ReportFilters1.Visible = true;
            ReportFilters1.ReportName = FirstLevelList.SelectedValue.Split('=')[1];
            }
            else ReportFilters1.Visible = false;

        } 
        catch (Exception ex)
        {
            PropertyFilters.ErrorMessage = ex.Message;
        }
        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
        Session["SecondLevelList.DataSource"] = SecondLevelList.DataSource;
        Session["PropertiesForName"] = FirstLevelList.SelectedValue;

    }

    private void NoPropertyList()
    {
        lblSecondLevel.Text = "";
        SecondLevel.Visible = false;
        SecondLevelList.Items.Clear();


    }

    protected void SecondLevelList_SelectedIndexChanged(object sender, EventArgs e)
    {
        imgbtnRefresh2.Visible = SecondLevelList.SelectedIndex > 0;
        Session["PropertyName"] = SecondLevelList.SelectedItem.Text;
    }

    protected void SecondLevelList_DataBound(object sender, EventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");
        DropDownList LB = (DropDownList)sender;
        lblSecondLevel.Text = String.Format("Properties ({0:n0})", LB.Items.Count - 1);
        lblSecondLevel.ToolTip = string.Format("{0} {1}", LB.Items.Count - 1, (new Menus()).FilterLegend("Properties", FirstLevelList.SelectedValue));
        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    protected void FirstLevelList_DataBound(object sender, EventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");
        DropDownList LB = (DropDownList)sender;
        string sFirst = EntryPointSelect.SelectedValue+ "s";
        int nOffSet =  sFirst == "States" ? 6 : sFirst == "Regions" ? 1 : sFirst == "Managers" ? 5 : sFirst == "Markets" ? 5 : 0;
        nOffSet += 1;
        if (LB.Items.Count < nOffSet) nOffSet = LB.Items.Count;
 
        lblFirstLevel.Text = sFirst + " (" + (LB.Items.Count - nOffSet).ToString() + ")";
        if (EntryPointSelect.SelectedValue.StartsWith("Portfolio"))
            lblFirstLevel.ToolTip = string.Format("{0} {1}", LB.Items.Count - 1, (new Menus()).FilterLegend("Portfolios", ""));
        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    protected void chkByTracID_CheckedChanged(object sender, EventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");

        string sTracID = SecondLevelList.SelectedValue;
        ((RollupMetadata)HttpContext.Current.Session["RollupMetadata"]).ByTracId = chkByTracID.Checked;
        FirstLevelList_SelectedIndexChanged(FirstLevelList, e);
        SecondLevelList.Items.FindByValue(sTracID).Selected = true;
        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    protected void imgbtnShow3_Click(object sender, EventArgs e)
    {
        ThirdLevel.Style["display"] = ThirdLevel.Style["display"] == "" ? "none" : "";
        //ThirdLevel.Visible = !ThirdLevel.Visible;
    }

    protected void imgbtnRefresh1_Click(object sender, ImageClickEventArgs e)
    {
        RollupFilters1.RefreshMetadata(sender, e);
    }

    protected void imgbtnRefresh2_Click(object sender, ImageClickEventArgs e)
    {
        RollupFilters1.RefreshMetadata(sender, e);
    }

    protected void imgbtnShow3_Click(object sender, ImageClickEventArgs e)
    {

    }

    protected void imgbtnFilter_Click(object sender, ImageClickEventArgs e)
    {
        FourthLevel.Visible = !FourthLevel.Visible;
        ThirdLevel.Style["display"] = "none";
        if (FourthLevel.Visible)
        {
            switch (((System.Web.UI.Control)(sender)).ID)
            {
                case "imgbtnFilterPortfolios":
                    PortfolioFilters1.Visible = true;
                    PropertyFilters.Visible = false;
                    break;
                case "imgbtnFilterProperties":
                    PropertyFilters.Visible = true;
                    PortfolioFilters1.Visible = false;
                    break;
                default:
                    break;
            }
        }
    }

    protected void odsFirstLevel_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        lblFirstLevel.Text = EntryPointSelect.SelectedValue + "(" + FirstLevelList.Items.Count.ToString() + ")";
    }
}
